package cn.sharp.bbs.controller.user;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.common.page.PageParam;
import cn.sharp.bbs.entity.BO.CurrentUser;
import cn.sharp.bbs.entity.DO.Group;
import cn.sharp.bbs.service.UserFollowService;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/follow")
public class FollowController {
    @Resource
    UserFollowService followService;

    /**
     * 获取用户近期关注列表
     * @param account 用户账号
     * @return 关注列表, 只返回最新的10个
     */
    @GetMapping("/recent")
    public Result getFollows(
            @Nullable @RequestParam("account")String account
    ){
        return Result.success(followService.getRecentFollows(account));
    }

    /**
     * 获取用户关注分组
     * @param currentUser 当前用户, 从Token中获得
     * @return 该用户的关注分组列表
     */
    @GetMapping("/group")
    public Result getFollowGroups(
            @RequestAttribute("CurrentUser") CurrentUser currentUser
    ){
        if(currentUser==null)
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        return Result.success(followService.getFollowLists(currentUser.getU_id()));
    }

    /**
     * 分页获取默认分组中的关注者
     * @param currentUser 当前用户, 从Token中获得
     * @param pageParam 分页参数
     * @return 分页的查询结果
     */
    @GetMapping("/group/all")
    public Result getAllFollow(
            @Nullable @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @Nullable PageParam pageParam
    ){
        if(currentUser==null)
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        if(pageParam==null) pageParam=new PageParam();
        return Result.success(followService.getFollowInGroup(currentUser.getU_id(), null, pageParam));
    }

    /**
     * 分页获取关注分组中的关注者
     * @param currentUser 当前用户, 从Token中获得
     * @param group 关注分组id
     * @param pageParam 分页参数
     * @return 分页的查询结果
     */
    @GetMapping("/group/{group}")
    public Result getFollowInGroup(
            @Nullable @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @PathVariable Long group,
            @Nullable PageParam pageParam
    ){
        if(currentUser==null)
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        if(followService.isInvalidGroup(currentUser.getU_id(), group))
            return Result.fail(Result.ERR_CODE_BUSINESS, "分组错误");
        if(pageParam==null) pageParam=new PageParam();
        return Result.success(followService.getFollowInGroup(currentUser.getU_id(), group, pageParam));
    }

    /**
     * 将用户移动至分组
     * @param currentUser 当前用户, 从Token中获得
     * @param group 目标分组id
     * @param userIds 要移动的用户id
     * @return 移动的结果
     */
    @PostMapping("/group/{group}/add/ByCopy")
    public Result copyFollowsToGroup(
            @RequestAttribute CurrentUser currentUser,
            @PathVariable Long group,
            @RequestBody Long[] userIds
    ){
        if(currentUser==null)
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        if(followService.isInvalidGroup(currentUser.getU_id(), group))
            return Result.fail(Result.ERR_CODE_BUSINESS, "分组错误");
        String payload = followService.copyFollowToGroup(currentUser.getU_id(), userIds, group);
        if(payload!=null) return Result.fail(Result.ERR_CODE_BUSINESS, payload);
        return Result.success();
    }

    /**
     * 获得关注关系所在的分组
     * @param currentUser 当前用户, 从Token中获得
     * @param u_id 关注关系的目标用户id
     * @return 分组列表
     */
    @GetMapping("/user/{u_id}/group")
    public Result getUserInGroup(
            @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @PathVariable Long u_id
    ){
        if(currentUser==null)
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        return Result.success(followService.getUserInGroup(currentUser.getU_id(), u_id));
    }

    /**
     * 修改用户的分组
     * @param currentUser 当前用户, 从Token中获得
     * @param u_id 要修改的目标用户
     * @param groups 群组列表
     * @return 修改结果
     */
    @PostMapping("/user/{u_id}/group")
    public Result changeUserToGroup(
            @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @PathVariable Long u_id,
            @RequestBody Long[] groups
    ){
        if(currentUser==null)
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        String payload = followService.setFollowToGroup(currentUser.getU_id(), u_id, groups);
        if(payload!=null)
            return Result.fail(Result.ERR_CODE_BUSINESS, payload);
        return Result.success();
    }

    /**
     * 删除关注分组
     * @param currentUser 当前用户, 从Token中获得
     * @param group 要删除的分组id
     * @return 删除结果
     */
    @PostMapping("/group/{group}/del")
    public Result deleteGroup(
            @RequestAttribute CurrentUser currentUser,
            @PathVariable Long group
    ){
        if(currentUser==null)
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        if(followService.isInvalidGroup(currentUser.getU_id(), group))
            return Result.fail(Result.ERR_CODE_BUSINESS, "分组错误");
        String payload = followService.delFollowGroup(currentUser.getU_id(), group);
        if(payload!=null)
            return Result.fail(Result.ERR_CODE_BUSINESS, payload);
        return Result.success();
    }

    /**
     * 创建关注分组
     * @param currentUser 当前用户, 从Token中获得
     * @param group 要创建的分组信息
     * @return 创建的结果
     */
    @PostMapping("/group")
    public Result createGroup(
            @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @RequestBody Group group
    ){
        if(currentUser==null)
            return Result.fail(Result.ERR_CODE_BUSINESS, "无权操作");
        group.setUserId(currentUser.getU_id());
        followService.createGroup(group);
        return Result.success(group);
    }
}
